package com.castle.fortress.admin.oa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.dto.AttendanceRecordDto;
import com.castle.fortress.admin.oa.entity.DayStatisticsEntity;
import com.castle.fortress.admin.oa.dto.DayStatisticsDto;
import com.castle.fortress.admin.oa.service.DayStatisticsService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;

import javax.servlet.http.HttpServletResponse;

import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.List;

/**
 * 日数据报表 控制器
 *
 * @author whc
 * @since 2022-11-04
 */
@Api(tags = "日数据报表管理控制器")
@Controller
public class DayStatisticsController {
    @Autowired
    private DayStatisticsService dayStatisticsService;

    /**
     * 日数据报表的分页展示
     *
     * @param dayStatisticsDto 日数据报表实体类
     * @param current          当前页
     * @param size             每页记录数
     * @return
     */
    @CastleLog(operLocation = "日数据报表分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("日数据报表分页展示")
    @GetMapping("/oa/dayStatistics/page")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:pageList")
    public RespBody<IPage<DayStatisticsDto>> pageDayStatistics(DayStatisticsDto dayStatisticsDto, @RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        Integer pageIndex = current == null ? GlobalConstants.DEFAULT_PAGE_INDEX : current;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<DayStatisticsDto> page = new Page(pageIndex, pageSize);
        if (!WebUtil.currentUser().getIsAdmin() && !WebUtil.currentUser().getIsSuperAdmin()) {
            final List<Long> authDept = WebUtil.currentUser().getAuthDept();
            if (authDept == null || authDept.size() <= 0) {
                Page<DayStatisticsDto> pageDto = new Page(page.getCurrent(), page.getSize(), 0);
                pageDto.setRecords(null);
                return RespBody.data(pageDto);
            }
            dayStatisticsDto.setAuthDept(authDept);
        }
        IPage<DayStatisticsDto> pages = dayStatisticsService.pageDayStatisticsExtends(page, dayStatisticsDto);
        return RespBody.data(pages);
    }

    /**
     * 日数据报表的列表展示
     *
     * @param dayStatisticsDto 日数据报表实体类
     * @return
     */
    @CastleLog(operLocation = "日数据报表列表展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("日数据报表列表展示")
    @GetMapping("/oa/dayStatistics/list")
    @ResponseBody
    public RespBody<List<DayStatisticsDto>> listDayStatistics(DayStatisticsDto dayStatisticsDto) {
        List<DayStatisticsDto> list = dayStatisticsService.listDayStatistics(dayStatisticsDto);
        return RespBody.data(list);
    }

    /**
     * 日数据报表保存
     *
     * @param dayStatisticsDto 日数据报表实体类
     * @return
     */
    @CastleLog(operLocation = "日数据报表保存", operType = OperationTypeEnum.INSERT)
    @ApiOperation("日数据报表保存")
    @PostMapping("/oa/dayStatistics/save")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:save")
    public RespBody<String> saveDayStatistics(@RequestBody DayStatisticsDto dayStatisticsDto) {
        if (dayStatisticsDto == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto, DayStatisticsEntity.class);
        if (dayStatisticsService.save(dayStatisticsEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日数据报表编辑
     *
     * @param dayStatisticsDto 日数据报表实体类
     * @return
     */
    @CastleLog(operLocation = "日数据报表编辑", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("日数据报表编辑")
    @PostMapping("/oa/dayStatistics/edit")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:edit")
    public RespBody<String> updateDayStatistics(@RequestBody DayStatisticsDto dayStatisticsDto) {
        if (dayStatisticsDto == null || dayStatisticsDto.getId() == null || dayStatisticsDto.getId().equals(0L)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto, DayStatisticsEntity.class);
        if (dayStatisticsService.updateById(dayStatisticsEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日数据报表删除
     *
     * @param id
     * @return
     */
    @CastleLog(operLocation = "日数据报表删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("日数据报表删除")
    @PostMapping("/oa/dayStatistics/delete")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:delete")
    public RespBody<String> deleteDayStatistics(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (dayStatisticsService.removeById(id)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 日数据报表批量删除
     *
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "日数据报表批量删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("日数据报表批量删除")
    @PostMapping("/oa/dayStatistics/deleteBatch")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:deleteBatch")
    public RespBody<String> deleteDayStatisticsBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (dayStatisticsService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 日数据报表详情
     *
     * @param id 日数据报表id
     * @return
     */
    @CastleLog(operLocation = "日数据报表详情", operType = OperationTypeEnum.QUERY)
    @ApiOperation("日数据报表详情")
    @GetMapping("/oa/dayStatistics/info")
    @ResponseBody
    @RequiresPermissions("oa:dayStatistics:info")
    public RespBody<DayStatisticsDto> infoDayStatistics(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        DayStatisticsDto dayStatisticsDto = dayStatisticsService.getByIdExtends(id);
        return RespBody.data(dayStatisticsDto);
    }

    /**
     * 动态表头导出 依据展示的字段导出对应报表
     *
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "日数据报表导出", operType = OperationTypeEnum.EXPORT)
    @PostMapping("/oa/dayStatistics/exportDynamic")
    @ApiOperation("动态表头导出，依据展示的字段导出对应报表")
    public void exportDynamic(@RequestBody DynamicExcelEntity<DayStatisticsDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
        List<DayStatisticsDto> list = dayStatisticsService.listDayStatistics(dynamicExcelEntity.getDto());
        //字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
        List<List<Object>> dataList = null;
        /**
         * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
         */
        ExcelUtils.exportDynamic(response, dynamicExcelEntity.getFileName() + ".xlsx", null, list, dynamicExcelEntity.getHeaderList(), dataList);
    }


}
